//carla.js
const util = require('../../utils/util.js')
const app = getApp();
const config = require('../../config.js');

/**
 * 定时
 */
function doTime(params) {
  console.log(params.startTime);
  console.log(params.endTime);
  console.log(params.imei);
  console.log(params.openid);
  console.log(params.token);
  console.log(params.timeType);
  wx.showToast({
    title: '正在定时...',
    icon: 'loading'
  });
  wx.request({
    //后台接口地址
    url: config.serverUrl + '/web/api/inner/opr/setTime',
    data: params,
    method: 'POST',
    header: {
      'content-type': 'application/x-www-form-urlencoded'
    },
    success: function (res) {
      wx.hideToast();
      if (res) {
        var respData = res.data;
        if (respData.code == '000000') {
          wx.showToast({
            title: '定时成功!',
            icon: 'none',
            duration: 5000
          })
        } else if (respData.code == '000003') {
          wx.showToast({
            title: '请退出小程序在登录!',
            icon: 'none',
            duration: 5000
          })
        } else {
          console.log("********fail********");
          console.log(res)
          console.log("*********fail*******");
          wx.showToast({
            title: '定时失败!',
            icon: 'none',
            duration: 5000
          })
        }
      } else {
        console.log("********fail********");
        console.log(res)
        console.log("*********fail*******");
        wx.showToast({
          title: '定时失败!',
          icon: 'none',
          duration: 5000
        })
      }
    },
    fail: res => {
      wx.hideToast();
      console.log("********fail********");
      console.log(res)
      console.log("*********fail*******");
      wx.showToast({
        title: '定时失败!',
        icon: 'none',
        duration: 5000
      })
    }
  });
}

/**
 * 调档
 */
function doGear(params) {
  console.log(params.gearIndex);
  console.log(params.imei);
  console.log(params.openid);
  console.log(params.token);
  wx.showToast({
    title: '正在调档...',
    icon: 'loading'
  });
  wx.request({
    //后台接口地址
    url: config.serverUrl + '/web/api/inner/opr/setGear',
    data: params,
    method: 'POST',
    header: {
      'content-type': 'application/x-www-form-urlencoded'
    },
    success: function (res) {
      wx.hideToast();
      if (res) {
        var respData = res.data;
        if (respData.code == '000000') {
          wx.showToast({
            title: '调档成功!',
            icon: 'none',
            duration: 5000
          })
        } else if (respData.code == '000003') {
          wx.showToast({
            title: '请退出小程序在登录!',
            icon: 'none',
            duration: 5000
          })
        } else {
          console.log("********fail********");
          console.log(res)
          console.log("*********fail*******");
          wx.showToast({
            title: '调档失败!',
            icon: 'none',
            duration: 5000
          })
        }
      } else {
        console.log("********fail********");
        console.log(res)
        console.log("*********fail*******");
        wx.showToast({
          title: '调档失败!',
          icon: 'none',
          duration: 5000
        })
      }
    },
    fail: res => {
      wx.hideToast();
      console.log("********fail********");
      console.log(res)
      console.log("*********fail*******");
      wx.showToast({
        title: '调档失败!',
        icon: 'none',
        duration: 5000
      })
    }
  });
}

function initData(that, qrData) {
  var date = new Date();
  var hour = date.getHours();
  var min = date.getMinutes();
  var tmpHour;
  var tmpMin;
  if (hour < 10) {
    tmpHour = "0" + hour;
  } else {
    tmpHour = hour + "";
  }
  if (min < 10) {
    tmpMin = "0" + min;
  } else {
    tmpMin = min + "";
  }
  that.setData({
    startTimeOne: tmpHour + ":" + tmpMin,
    startTimeTow: tmpHour + ":" + tmpMin,
    startTimeThree: tmpHour + ":" + tmpMin,
    qrData: decodeURIComponent(qrData)
  })
  console.log("----------------")
  console.log(that.data)
  console.log("----------------")
}

//获取设备在线的信息
function getWorkDeivceInfo(that, openid, imei, token) {
  wx.showToast({
    title: '获取信息...',
    icon: 'loading'
  });
  wx.request({
    //后台接口地址
    url: config.serverUrl + '/web/api/inner/opr/getDeviceWorkInfo',
    data: {
      openid: openid,
      imei: imei,
      token: token
    },
    method: 'POST',
    header: {
      'content-type': 'application/x-www-form-urlencoded'
    },
    success: function (res) {
      wx.hideToast();
      if (res) {
        var respData = res.data;
        if (respData) {
          if (respData.code == '000000') {
            var info = respData.info;
            if (info) {
                console.log(info)
                that.setData({
                  startTimeOne: info.work_start_time1,
                  endTimeOne: info.work_close_time1,
                  startTimeTow: info.work_start_time2,
                  endTimeTow: info.work_close_time2,
                  startTimeThree: info.work_start_time3,
                  endTimeThree: info.work_close_time3,
                  qrData: imei,
                  gearIndex:info.gear
                })
            } else {
              initData(that, imei);
            }
          } else if (respData.code == '000003') {
            wx.showToast({
              title: '请退出小程序在登录!',
              icon: 'none',
              duration: 5000
            })
          } else {
            console.log("********fail********");
            console.log(res)
            console.log("*********fail*******");
            initData(that, imei);
          }
        } else {
          console.log("********fail********");
          console.log(res)
          console.log("*********fail*******");
          initData(that, imei);
        }
      } else {
        console.log("********fail********");
        console.log(res)
        console.log("*********fail*******");
        initData(that, imei);
      }
    },
    fail: res => {
      console.log("********fail********");
      console.log(res)
      console.log("*********fail*******");
      wx.hideToast();
      initData(that, imei);
    }
  });
}

Page({
  data: {
    qrData: null,
    infoFlag: false,
    imgUrls: [{
      url: config.serverUrl + "/web/api/h1.jpg",
      key: "image1"
    }, {
      url: config.serverUrl + "/web/api/h2.jpg",
      key: "image2"
    }, {
      url: config.serverUrl + "/web/api/h3.jpg",
      key: "image3"
    }],
    indicatorDots: false,
    autoplay: true,
    interval: 5000,
    duration: 1000,
    startTimeOne: '00:00',
    endTimeOne: '23:59',
    startTimeTow: '00:00',
    endTimeTow: '23:59',
    startTimeThree: '00:00',
    endTimeThree: '23:59',
    gears: ["一档", "二档", "三档", "四档", "五档", "六档"],
    gearIndex: 0
  },
  binGearChange: function (e) {
    console.log('picker gear 发生选择改变，携带值为', e.detail.value);
    this.setData({
      gearIndex: e.detail.value
    })
  },
  bindTimeChangeStartOne: function (e) {
    console.log('picker One 发送选择改变，携带值为', e.detail.value)
    this.setData({
      startTimeOne: e.detail.value
    })
  },
  bindTimeChangeEndOne: function (e) {
    console.log('picker One发送选择改变，携带值为', e.detail.value)
    this.setData({
      endTimeOne: e.detail.value
    })
  },
  bindTimeChangeStartTow: function (e) {
    console.log('picker Tow发送选择改变，携带值为', e.detail.value)
    this.setData({
      startTimeTow: e.detail.value
    })
  },
  bindTimeChangeEndTow: function (e) {
    console.log('picker Tow发送选择改变，携带值为', e.detail.value)
    this.setData({
      endTimeTow: e.detail.value
    })
  },
  bindTimeChangeStartThree: function (e) {
    console.log('picker Three发送选择改变，携带值为', e.detail.value)
    this.setData({
      startTimeThree: e.detail.value
    })
  },
  bindTimeChangeEndThree: function (e) {
    console.log('picker Three发送选择改变，携带值为', e.detail.value)
    this.setData({
      endTimeThree: e.detail.value
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    if (options.infoFlag) {
      //获取在线的设备的信息
      var imei = decodeURIComponent(options.qrData);
      var openid = app.globalData.openid;
      var token = app.globalData.token;
      getWorkDeivceInfo(that, openid, imei, token);
    } else {
      initData(that, options.qrData);
    }
  },
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    console.log("onShareAppMessage");
    return {
      title: '香氛机',
      path: '/pages/index/index',
      imageUrl: '../../images/share.png'
    }
  },
  doSubmitOne: function () {
    var paramObj = {
      startTime: this.data.startTimeOne,
      endTime: this.data.endTimeOne,
      imei: this.data.qrData,
      openid: app.globalData.openid,
      token: app.globalData.token,
      timeType: "one",
    }
    doTime(paramObj);
  },
  doSubmitTow: function () {
    var paramObj = {
      startTime: this.data.startTimeTow,
      endTime: this.data.endTimeTow,
      imei: this.data.qrData,
      openid: app.globalData.openid,
      token: app.globalData.token,
      timeType: "tow",
    }
    doTime(paramObj);
  },
  doSubmitThree: function () {
    var paramObj = {
      startTime: this.data.startTimeThree,
      endTime: this.data.endTimeThree,
      imei: this.data.qrData,
      openid: app.globalData.openid,
      token: app.globalData.token,
      timeType: "three",
    }
    doTime(paramObj);
  },
  doSubmitGear: function () {
    var paramObj = {
      gearIndex: this.data.gearIndex,
      imei: this.data.qrData,
      openid: app.globalData.openid,
      token: app.globalData.token,
    }
    doGear(paramObj)
  },
  doCancel: function () {
    //取消
    wx.navigateBack({
      url: "../index/index"
    });
  },
  doShutdown: function () {
    //关机
    var imei = this.data.qrData;
    var openid = app.globalData.openid;
    var token = app.globalData.token;
    console.log(imei);
    console.log(openid);
    wx.showToast({
      title: '正在关机...',
      icon: 'loading'
    });
    wx.request({
      //后台接口地址
      url: config.serverUrl + '/web/api/inner/opr/closeCarla',
      data: {
        openid: openid,
        imei: imei,
        token: token,
      },
      method: 'POST',
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      success: function (res) {
        wx.hideToast();
        if (res) {
          var respData = res.data;
          if (respData) {
            if (respData.code == '000000') {
              wx.showToast({
                title: '关机成功!',
                icon: 'none',
                duration: 5000
              });
              wx.navigateTo({
                url: "../device/device",
                success: function (res) {
                  console.log('跳转页面成功')
                },
                fail: function (res) {
                  console.log(res)
                  console.log('跳转页面失败')
                }
              });
            } else if (respData.code == '000003') {
              wx.showToast({
                title: '请退出小程序在登录!',
                icon: 'none',
                duration: 5000
              })
            } else {
              console.log("********fail********");
              console.log(res)
              console.log("*********fail*******");
              wx.showToast({
                title: '关机失败!',
                icon: 'none',
                duration: 5000
              })
            }
          } else {
            console.log("********fail********");
            console.log(res)
            console.log("*********fail*******");
            wx.showToast({
              title: '关机失败!',
              icon: 'none',
              duration: 5000
            })
          }
        } else {
          console.log("********fail********");
          console.log(res)
          console.log("*********fail*******");
          wx.showToast({
            title: '关机失败!',
            icon: 'none',
            duration: 5000
          })
        }
      },
      fail: res => {
        console.log("********fail********");
        console.log(res)
        console.log("*********fail*******");
        wx.hideToast();
        wx.showToast({
          title: '关机失败!',
          icon: 'none',
          duration: 5000
        })
      }
    });
  }
})
